package cn.neusoft.luofa603.yuejian_29.fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.activity.MasterVideoActivity;
import cn.neusoft.luofa603.yuejian_29.adapter.MasterVideoAdapter;
import cn.neusoft.luofa603.yuejian_29.bean.Master;
import cn.neusoft.luofa603.yuejian_29.bean.Mastervideo;
import cn.neusoft.luofa603.yuejian_29.model.MasterVideoModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MasterVideoFragment extends Fragment {
    private View view;
    private String SessionID,id;
    private Master master;
    private RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mastervideo, container, false);
        initViews();
        initEvents();
        initData();
        return view;
    }
    public void initViews(){
        recyclerView = (RecyclerView) view.findViewById(R.id.master_video_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        DividerItemDecoration decoration = new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL);
        decoration.setDrawable(ContextCompat.getDrawable(getContext(),R.drawable.line));
        recyclerView.addItemDecoration(decoration);

    }
    public void initEvents(){

    }
    public void initData(){
        get_SessionID();
        getMater();
        get_master_video();
    }
    public void get_SessionID(){
        SharedPreferences sp= getActivity().getSharedPreferences("sessionid", Context.MODE_PRIVATE);
        SessionID=sp.getString("sessionid", null);

    }
    public void getMater(){
        final Bundle bundle=getActivity().getIntent().getExtras();
        master = (Master) bundle.getSerializable("Master");
        id=master.getId();
    }

    public void get_master_video(){
       MasterVideoModel masterVideoModel=new MasterVideoModel();
        Call<Mastervideo> call=masterVideoModel.getvideo("master","1",id,"mvideo");
        call.enqueue(new Callback<Mastervideo>() {
            @Override
            public void onResponse(Call<Mastervideo> call, Response<Mastervideo> response) {
                final Mastervideo mastervideo = response.body();
                MasterVideoAdapter masterVideoAdapter = new MasterVideoAdapter(getActivity(),mastervideo.getMvideo());
                recyclerView.setAdapter(masterVideoAdapter);

            }

            @Override
            public void onFailure(Call<Mastervideo> call, Throwable t) {
                t.printStackTrace();

            }
        });

    }

}
